package cn.dada.dadarecipes.user.order.service.impl;


import cn.dada.dadarecipes.user.order.mapper.OrderMapper;
import cn.dada.dadarecipes.user.order.pojo.dto.OrderPayDto;
import cn.dada.dadarecipes.user.order.pojo.dto.OrderUpdateParam;
import cn.dada.dadarecipes.user.order.pojo.dto.QueryOrderParam;
import cn.dada.dadarecipes.user.order.pojo.entity.Order;
import cn.dada.dadarecipes.user.order.pojo.vo.OrderVO;
import cn.dada.dadarecipes.user.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Collections;
import java.util.Date;
import java.util.List;



@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderMapper orderMapper;


    @Override
    public List<OrderVO> queryOrders(QueryOrderParam queryOrderParam) {
        log.debug("查询订单: queryOrderParam={}", queryOrderParam);


        // 参数校验
        if (queryOrderParam == null) {
            log.error("查询参数不能为空");
            throw new IllegalArgumentException("查询参数不能为空");
        }

        if (queryOrderParam.getUserId() == null) {
            log.error("用户ID不能为空");
            throw new IllegalArgumentException("用户ID不能为空");
        }

        if (queryOrderParam.getShippingAddressId() == null) {
            log.warn("地址ID未指定，将不会基于地址ID过滤订单");
            // 可选操作：这里可以选择抛出异常，或者只是记录警告并继续执行
        }
        // 调用Mapper层进行查询
        List<OrderVO> orderVOs = orderMapper.queryOrdersByParams(queryOrderParam);

        // 如果没有找到任何订单，返回空列表
        if (orderVOs == null) {
            orderVOs = java.util.Collections.emptyList();
        }

        return orderVOs;
    }

    @Override
    public List<OrderVO> selectOrderChecked(Long userId) {
        QueryOrderParam queryOrderParam = new QueryOrderParam();
        queryOrderParam.setUserId(userId);
        queryOrderParam.setOrderStatus("5");
        return queryOrders(queryOrderParam);
    }

    @Override
    public void payOrder(OrderPayDto orderpayDto) {
        Order order = new Order();
        order.setId(orderpayDto.getId());
        order.setPayTime(new Date());
        order.setShippingAddressId(orderpayDto.getAddressId());
        order.setOrderStatus("10");
        orderMapper.update(order);
    }


    @Override
    public void update(OrderUpdateParam orderUpdateParam) {
        log.debug("订单更新:orderVO={}", orderUpdateParam);
        Order order = new Order();
        order.setId(orderUpdateParam.getId());// 设置订单ID
        order.setOrderStatus(orderUpdateParam.getOrderStatus());// 设置订单状态
        order.setShippingStatus(orderUpdateParam.getShippingStatus()); // 设置发货状态
        order.setPayTime(orderUpdateParam.getPayTime());// 设置支付时间
        order.setDispatchTime(orderUpdateParam.getDispatchTime()); // 设置发货时间
        order.setTransactionTime(orderUpdateParam.getTransactionTime()); // 设置交易完成时间
        order.setUpdateTime(orderUpdateParam.getUpdateTime());// 设置更新时间
        orderMapper.update(order);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除订单:id={}", id);
        orderMapper.delete(id);
    }
}
